package study.saas.web.portal.sys.admin.Controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import study.saas.common.viewmodel.ApiResultV2;
import study.saas.order.entity.Order;
import study.saas.order.service.OrderService;
import study.saas.order.service.model.OrderCriteria;


import javax.annotation.Resource;
import java.io.File;
import java.util.List;

@RestController
@RequestMapping("/order")
@Slf4j
@Api("订单业务实现")
public class OrderController {
    @Resource
    private OrderService orderService;

    //功能一：创建订单
    @PostMapping("/add")
    @ApiOperation("创建订单")
    public ApiResultV2 addOrder(@RequestBody Order order) {
        log.info("创建订单:{}",order.toString());
        orderService.createOrder(order);
        return ApiResultV2.ok();
    }

    //功能二：根据id查询订单
    @GetMapping("/{id}")
    @ApiOperation("根据id查询订单")
    public ApiResultV2<Order> getOrderById(@PathVariable Long id) {
        Order order = orderService.findById(id);
        return ApiResultV2.ok(order);
    }

    //功能三：条件查询订单
    @GetMapping("/criteria")
    @ApiOperation("条件查询订单")
    public ApiResultV2<List<Order>> getOrderByCriteria(@RequestBody OrderCriteria criteria) {
        List<Order> ordersByCriteria = orderService.findOrdersByCriteria(criteria);
        return ApiResultV2.ok(ordersByCriteria);
    }

    //功能四：导出订单数据
    @GetMapping("/export")
    @ApiOperation("导出订单数据")
    public ApiResultV2<File> exportOrders(@RequestBody OrderCriteria criteria) {
        File file = orderService.exportOrders(criteria);
        return ApiResultV2.ok(file);
    }
}
